package cn.ly.json.gsontest;

import com.google.common.reflect.TypeToken;
import com.google.gson.*;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by liuyu21 on 2017/12/14.
 */
public class GsonTest {

    public static void main(String[] args) {
        //String json = "{ \"name\":\"java书籍\", \"authors\":[\"Jerry\",\"Tom\"], \"price\":\"13\"}";
        /*JsonParser parser = new JsonParser();
        JsonElement ele = parser.parse(json);
        System.out.println(ele);
        System.out.println(ele.getAsJsonObject().get("authors").isJsonArray());*/

        String str = "{\"aaa\":\"1\"}";

        /*Gson gson = new GsonBuilder().
            registerTypeAdapter(Double.class, new JsonSerializer<Double>() {
                @Override
                public JsonElement serialize(Double aDouble, Type type, JsonSerializationContext jsonSerializationContext) {
                    if (aDouble == aDouble.longValue())
                        return new JsonPrimitive(aDouble.longValue());
                    return new JsonPrimitive(aDouble);
                }
            }).create();*/
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
        Map<String,Object> resultMap = gson.fromJson(str,new TypeToken<HashMap>(){}.getType());
        System.out.println(resultMap.get("aaa"));
    }

}
